package com.whut.cristiano.autographbook.Util;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.whut.cristiano.autographbook.Activity.MainActivity;
import com.whut.cristiano.autographbook.Model.CircleAdapter;
import com.whut.cristiano.autographbook.Model.GsonRequest;
import com.whut.cristiano.autographbook.Model.MyCircleAdapter;
import com.whut.cristiano.autographbook.Model.NetAccessUrl;
import com.whut.cristiano.autographbook.Model.ReceiveCircleAdapter;
import com.whut.cristiano.autographbook.MyApplication;
import com.whut.cristiano.autographbook.VO.BaseResultVO;
import com.whut.cristiano.autographbook.VO.CardVO;
import com.whut.cristiano.autographbook.VO.CircleItem;
import com.whut.cristiano.autographbook.VO.CircleItemVO;
import com.whut.cristiano.autographbook.VO.LoginResultVO;
import com.whut.cristiano.autographbook.VO.SchoolResVO;
import com.whut.cristiano.autographbook.VO.UserVO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ykbboom on 2017/3/31 13:27
 * email：ykbpro@whut.edu.cn
 */

public class NetUtil {
    private static final String TAG = NetUtil.class.getSimpleName();
    private static RequestQueue requestQueue = MyApplication.getRequestQueue();
    private static Context context = MyApplication.getContext();

    public static void getSchoolByProvince(String provinceName, final ListView schoolListView){
        Map<String,String> params=new HashMap<String, String>();
        params.put("provinceName",provinceName);

        GsonRequest<SchoolResVO> myGsonRequest = new GsonRequest<>(
                Request.Method.POST,
                NetAccessUrl.GET_SCHOOL,
                params,
                SchoolResVO.class,
                new Response.Listener<SchoolResVO>() {
                    @Override
                    public void onResponse(SchoolResVO schoolResVO) {
                        if (schoolResVO.getResultCode()==BaseResultVO.RESULT_CODE_FAIL){
                            LogUtil.d(TAG,"fail");
                            ToastUtil.showToast(context,schoolResVO.getMsg());
                        }else {
                            LogUtil.d(TAG,"get school data"+schoolResVO.getSchool().toString());
                            ArrayAdapter<String>adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,schoolResVO.getSchool());
                            schoolListView.setAdapter(adapter);
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        LogUtil.e(TAG,volleyError.toString());
                    }
                });
        requestQueue.add(myGsonRequest);
    }

    public static void RegAndLogin(final UserVO userVO, final Activity activity, final ProgressDialog progressDialog){
        Map<String,String> params=new HashMap<String, String>();
        params.put("userPhone",userVO.getUserPhone());
        params.put("userPass", userVO.getUserPass());
        params.put("userName", userVO.getUserName());
        params.put("userSchool", userVO.getUserSchool());
        GsonRequest<BaseResultVO>myGsonRequest = new GsonRequest<>(
                Request.Method.POST,
                NetAccessUrl.REG,
                params,
                BaseResultVO.class,
                new Response.Listener<BaseResultVO>() {
                    @Override
                    public void onResponse(BaseResultVO baseResultVO) {
                        LogUtil.i(TAG,"回调RegAndLogin onResponse()方法");
                        if (baseResultVO.getResultCode()==BaseResultVO.RESULT_CODE_FAIL){
                            ToastUtil.showToast(context,baseResultVO.getMsg());
                            progressDialog.dismiss();
                        }else {
                            //调用缓存将信息缓存下来
                            LogUtil.i(TAG,"Reg缓存信息");
                            SharedPrefUtil.saveUserName(userVO.getUserName());
                            SharedPrefUtil.saveUserPhone(userVO.getUserPhone());
                            SharedPrefUtil.saveUserSchool(userVO.getUserSchool());
                            SharedPrefUtil.saveLoginState(true);
                            SharedPrefUtil.saveUserPhoto("http://autographbook.oss-cn-hangzhou.aliyuncs.com/33114461_p3.jpg");

                            progressDialog.dismiss();
                            Intent intent = new Intent(activity, MainActivity.class);
                            activity.startActivity(intent);
                            activity.finish();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        LogUtil.e(TAG,volleyError.toString());
                    }
                });
        requestQueue.add(myGsonRequest);
    }
    public static void Login(final String userPhone, String userPass, final Activity activity,final ProgressDialog dialog){
        LogUtil.i(TAG,"phone:"+userPhone);
        LogUtil.i(TAG,"password:"+userPass);

        Map<String,String> params=new HashMap<String, String>();
        params.put("userPhone",userPhone);
        params.put("userPass",userPass);
        GsonRequest<LoginResultVO>myGsonRequest = new GsonRequest<LoginResultVO>(
                Request.Method.POST,
                NetAccessUrl.LOGIN,
                params,
                LoginResultVO.class,
                new Response.Listener<LoginResultVO>() {
                    @Override
                    public void onResponse(LoginResultVO loginResultVO) {
                        LogUtil.i(TAG,"回调Login onResponse()方法");
                        if (loginResultVO.getResultCode()==BaseResultVO.RESULT_CODE_FAIL){
                            ToastUtil.showToast(context,loginResultVO.getMsg());
                            dialog.dismiss();
                        }else {
                            LogUtil.d(TAG,loginResultVO.toString());
                            SharedPrefUtil.saveLoginState(true);
                            SharedPrefUtil.saveUserPhone(userPhone);
                            SharedPrefUtil.saveUserName(loginResultVO.getUserName());
                            SharedPrefUtil.saveUserSchool(loginResultVO.getUserSchool());
                            SharedPrefUtil.saveUserPhoto(loginResultVO.getUserPhoto());

                            dialog.dismiss();
                            Intent intent = new Intent(activity, MainActivity.class);
                            activity.startActivity(intent);
                            activity.finish();
                            ToastUtil.showToast(context,loginResultVO.getMsg());
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        LogUtil.e(TAG,volleyError.toString());
                    }
                });
        requestQueue.add(myGsonRequest);
    }
    public static void sendCard(String fromPhone,String toPhone, String picUrl,String time,final ProgressDialog progressDialog){
        Map<String,String> params=new HashMap<String, String>();
        params.put("fromPhone",fromPhone);
        params.put("toPhone", toPhone);
        params.put("picUrl", picUrl);
        params.put("time", time);
        GsonRequest<BaseResultVO>myGsonRequest = new GsonRequest<>(
                Request.Method.POST,
                NetAccessUrl.SEND_BOOK,
                params,
                BaseResultVO.class,
                new Response.Listener<BaseResultVO>() {
                    @Override
                    public void onResponse(BaseResultVO baseResultVO) {
                        if (baseResultVO.getResultCode()==BaseResultVO.RESULT_CODE_FAIL){
                            ToastUtil.showToast(context,baseResultVO.getMsg());
                            progressDialog.dismiss();
                        }else {
                            progressDialog.dismiss();
                            ToastUtil.showToast(context,baseResultVO.getMsg());
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        LogUtil.e(TAG,volleyError.toString());
                    }
                });
        requestQueue.add(myGsonRequest);
    }
    public static void updateUserInfo(String userPhone, String picUrl, String userName,final ProgressDialog progressDialog, final FloatingActionButton floatingActionButton){
        Map<String,String> params=new HashMap<>();
        params.put("userPhone",userPhone);
        params.put("picUrl", picUrl);
        params.put("userName", userName);
        GsonRequest<LoginResultVO>myGsonRequest = new GsonRequest<>(
                Request.Method.POST,
                NetAccessUrl.UPDATE_INFO,
                params,
                LoginResultVO.class,
                new Response.Listener<LoginResultVO>() {
                    @Override
                    public void onResponse(LoginResultVO loginResultVO) {
                        LogUtil.i(TAG,"回调RegAndLogin onResponse()方法");
                        if (loginResultVO.getResultCode()==BaseResultVO.RESULT_CODE_FAIL){
                            progressDialog.dismiss();
                            Snackbar.make(floatingActionButton,"操作未成功。",Snackbar.LENGTH_LONG).setAction("确定", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {}
                            }).show();
                        }else {
                            //调用缓存将信息缓存下来
                            SharedPrefUtil.saveUserName(loginResultVO.getUserName());
                            SharedPrefUtil.saveUserPhoto(loginResultVO.getUserPhoto());
                            SharedPrefUtil.saveUserPhotoTempPath(null);
                            progressDialog.dismiss();
                            Snackbar.make(floatingActionButton,"信息已修改。",Snackbar.LENGTH_LONG).setAction("确定", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {}
                            }).show();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        LogUtil.e(TAG,volleyError.toString());
                    }
                });
        requestQueue.add(myGsonRequest);
    }
    public static void getFriendCircle(final List<CircleItem> list, final CircleAdapter adapter, final ProgressDialog dialog){
        Map<String,String> params=new HashMap<>();
        GsonRequest<CircleItemVO>myGsonRequest = new GsonRequest<>(
                Request.Method.POST,
                NetAccessUrl.GET_FRIEND_CIRCLE,
                params,
                CircleItemVO.class,
                new Response.Listener<CircleItemVO>() {
                    @Override
                    public void onResponse(CircleItemVO circleItemVO) {

                        if (circleItemVO.getResultCode()==BaseResultVO.RESULT_CODE_FAIL){
                            ToastUtil.showToast(context,circleItemVO.getMsg());
                        }else {
                            for (CircleItem vo:circleItemVO.getCircleItemList()){
                                list.add(vo);
                            }
                            adapter.notifyDataSetChanged();
                            dialog.dismiss();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        LogUtil.e(TAG,volleyError.toString());
                    }
                });
        requestQueue.add(myGsonRequest);
    }
    public static void getMyCircle(String userPhone, final List<CircleItem> list, final MyCircleAdapter adapter, final ProgressDialog dialog){
        Map<String,String> params=new HashMap<>();
        params.put("userPhone",userPhone);
        GsonRequest<CircleItemVO>myGsonRequest = new GsonRequest<>(
                Request.Method.POST,
                NetAccessUrl.GET_MY_CIRCLE,
                params,
                CircleItemVO.class,
                new Response.Listener<CircleItemVO>() {
                    @Override
                    public void onResponse(CircleItemVO circleItemVO) {

                        if (circleItemVO.getResultCode()==BaseResultVO.RESULT_CODE_FAIL){
                            dialog.dismiss();
                            ToastUtil.showToast(context,circleItemVO.getMsg());
                        }else {
                            List<CircleItem>mList = circleItemVO.getCircleItemList();
                            for (CircleItem vo:mList){
                                CircleItem item = new CircleItem(vo.getCardId(),vo.getReplyFlag(),vo.getReplyContent(),vo.getReplyTime(),vo.getUserPhoto(),vo.getPicUrl(),vo.getTime(),vo.getName());
                                list.add(item);
                            }
                            LogUtil.d(TAG,list.toString());
                            adapter.notifyDataSetChanged();
                            dialog.dismiss();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        LogUtil.e(TAG,volleyError.toString());
                    }
                });
        requestQueue.add(myGsonRequest);
    }
    public static void replyCard(String replyContent,int cardId,String time,final ProgressDialog progressDialog){
        Map<String,String> params=new HashMap<String, String>();
        params.put("replyContent",replyContent);
        params.put("replyTime",time);
        params.put("cardId",String.valueOf(cardId));
        GsonRequest<BaseResultVO>myGsonRequest = new GsonRequest<>(
                Request.Method.POST,
                NetAccessUrl.UPDATE_REPLY,
                params,
                BaseResultVO.class,
                new Response.Listener<BaseResultVO>() {
                    @Override
                    public void onResponse(BaseResultVO baseResultVO) {
                        LogUtil.i(TAG,"回调RegAndLogin onResponse()方法");
                        if (baseResultVO.getResultCode()==BaseResultVO.RESULT_CODE_FAIL){
                            ToastUtil.showToast(context,baseResultVO.getMsg());
                            progressDialog.dismiss();
                        }else {
                            progressDialog.dismiss();
                            ToastUtil.showToast(context,baseResultVO.getMsg());
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        LogUtil.e(TAG,volleyError.toString());
                    }
                });
        requestQueue.add(myGsonRequest);
    }
    public static void getReceiveInfo(String userPhone, final List<CircleItem> list, final ReceiveCircleAdapter adapter, final ProgressDialog dialog){
        Map<String,String> params=new HashMap<>();
        params.put("userPhone",userPhone);
        GsonRequest<CircleItemVO>myGsonRequest = new GsonRequest<>(
                Request.Method.POST,
                NetAccessUrl.GET_RECEIVE_INFO,
                params,
                CircleItemVO.class,
                new Response.Listener<CircleItemVO>() {
                    @Override
                    public void onResponse(CircleItemVO circleItemVO) {

                        if (circleItemVO.getResultCode()==BaseResultVO.RESULT_CODE_FAIL){
                            dialog.dismiss();
                            ToastUtil.showToast(context,circleItemVO.getMsg());
                        }else {
                            List<CircleItem>mList = circleItemVO.getCircleItemList();
                            for (CircleItem vo:mList){
                                CircleItem item = new CircleItem(vo.getCardId(),vo.getReplyFlag(),vo.getReplyContent(),vo.getReplyTime(),vo.getUserPhoto(),vo.getPicUrl(),vo.getTime(),vo.getName());
                                list.add(item);
                            }
                            LogUtil.d(TAG,list.toString());
                            adapter.notifyDataSetChanged();
                            dialog.dismiss();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        LogUtil.e(TAG,volleyError.toString());
                    }
                });
        requestQueue.add(myGsonRequest);
    }

}
